package com.xphuong.task;

import android.os.AsyncTask;
import android.os.Handler;
import android.os.Message;

import com.xphuong.newskios.util.RssParser;
import com.xphuong.newskios.util.Util;

public class LoadNewsTask extends AsyncTask<String, Void, Boolean> {
	public static final int RESULT_OK = 0;
	public static final int RESULT_FAIL = 1;
	public static final int RESULT_CONNECTION_FAIL = 2;

	private Handler handler;

	public LoadNewsTask(Handler handler) {
		this.handler = handler;
	}

	@Override
	protected Boolean doInBackground(String... params) {
		try {
			String link = params[0];
			RssParser rssParser = new RssParser();
			Util.lstNews = rssParser.parser(link);

			if (Util.lstNews != null && Util.lstNews.size() > 0) {
				return true;
			} else
				return false;
		} catch (Exception e) {
			return false;
		}
	}

	@Override
	protected void onPostExecute(Boolean result) {
		Message msg = new Message();
		msg.what = Util.LOAD_NEWS_TASK;

		if (result) {
			msg.arg1 = RESULT_OK;
		} else {
			msg.arg1 = RESULT_FAIL;
		}

		if (handler != null)
			handler.sendMessage(msg);
	}
}